﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using FormsAuthenticationExtensions;


namespace Subplicity.Controllers
{
    public class BaseController : Controller
    {
        public string sourcename = "Subplicity";
        public string password = "WBL+nZlmq1YQbKRu0yKw2l0mTIg=";

        public string username = "michaelciav";
        public string userpassword = "mc08197711";
        public string email = "mikeciav@gmail.com";
        public int[] siteIDs = { -99 };

        public StaffService.Staff me;
        public int StaffID = 0;
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
          
            //Make sure user is logged in
            //Assign the staff object to the viewbag.me = staff
            try
            {
                if (User.Identity.IsAuthenticated)
                {
                    var ticketData = ((FormsIdentity)User.Identity).Ticket.GetStructuredUserData();

                    if (ticketData != null)
                    {
                        StaffID= Convert.ToInt32(ticketData["StaffID"]);
                        
                    }
                }

            }
            catch
            {
            }

        }

        public void CheckUserLogin(string loginusername, string loginPassword)
        {
            me = new StaffService.Staff();
            // Create Service
            StaffService.StaffService staffService = new StaffService.StaffService();
            // Create request
            StaffService.GetStaffRequest staffRequest = new StaffService.GetStaffRequest();
            staffRequest.SourceCredentials = new StaffService.SourceCredentials();
            staffRequest.SourceCredentials.SourceName = sourcename;
            staffRequest.SourceCredentials.Password = password;
            staffRequest.SourceCredentials.SiteIDs = siteIDs;

            staffRequest.StaffCredentials = new StaffService.StaffCredentials();
            staffRequest.StaffCredentials.Username = loginusername;
            staffRequest.StaffCredentials.Password = loginPassword;
            staffRequest.StaffCredentials.SiteIDs = siteIDs;
            staffRequest.Fields = new string[] { "ClassInstructor" };
            staffRequest.XMLDetail = StaffService.XMLDetailLevel.Basic;

            StaffService.GetStaffResult result = staffService.GetStaff(staffRequest);


            if (result.StaffMembers != null)
            {
                me = result.StaffMembers[0];
                ViewBag.Me = me;
            }
            else
                ViewBag.Me = null;

        }


    }
}
